package com.stylefeng.guns;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.xslf.usermodel.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * @author lmshe
 * @create 2017-11-24 15:46
 * @desc
 */
public class PPTtoHtml {
    /**
     * ppt03转html
     * filename:要读取的文件所在文件夹
     * filepath:文件名
     * htmlname:生成html名称
     * path:html存放路径
     * imgpath:图片存放路径
     * */
    public static void doPPTtoImage() throws TransformerException {
        String htmlname="exportPpt.html";
        // 读入PPT文件
        File file = new File("D:\\docx\\高中地理必修.ppt");
        boolean isppt = checkFile(file);
        if (!isppt) {
            System.out.println("The image you specify don't exit!");
        }
        try {

            FileInputStream is = new FileInputStream(file);
            SlideShow ppt = new SlideShow(is);
            is.close();
            Dimension pgsize = ppt.getPageSize();
            org.apache.poi.hslf.model.Slide[] slide = ppt.getSlides();
            FileOutputStream out =null;
            String imghtml="";
            for (int i = 0; i < slide.length; i++) {
                System.out.print("第" + i + "页。");

                TextRun[] truns = slide[i].getTextRuns();
                for ( int k=0;k<truns.length;k++){
                    RichTextRun[] rtruns = truns[k].getRichTextRuns();
                    for(int l=0;l<rtruns.length;l++){

                        rtruns[l].setFontIndex(1);
                        rtruns[l].setFontName("宋体");
                    }
                }
                BufferedImage img = new BufferedImage(pgsize.width,pgsize.height, BufferedImage.TYPE_INT_RGB);

                Graphics2D graphics = img.createGraphics();
                graphics.setPaint(Color.BLUE);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
                slide[i].draw(graphics);

                // 这里设置图片的存放路径和图片的格式(jpeg,png,bmp等等),注意生成文件路径
                out= new FileOutputStream("D:\\docx\\images\\" +(i + 1) + ".jpg");
                javax.imageio.ImageIO.write(img, "jpg", out);
                //图片在html加载路径
                String imgs="images/"+(i + 1) + ".jpg";
                imghtml+="<img src=\'"+imgs+"\' style=\'width:1200px;height:830px;vertical-align:text-bottom;\'><br><br><br><br>";

            }
            DOMSource domSource = new DOMSource();
            StreamResult streamResult = new StreamResult(out);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer serializer = tf.newTransformer();
            serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty(OutputKeys.METHOD, "html");
            serializer.transform(domSource, streamResult);
            out.close();
            String ppthtml="<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"+imghtml+"</body></html>";
            FileUtils.writeStringToFile(new File("D:\\docx\\", htmlname), ppthtml, "utf-8");
        } catch (FileNotFoundException e) {
            System.out.println(e);
            // System.out.println("Can't find the image!");
        } catch (IOException e) {
        }
    }

    // function 检查文件是否为PPT
    public static boolean checkFile(File file) {

        boolean isppt = false;
        String filename = file.getName();
        String suffixname = null;
        if (filename != null && filename.indexOf(".") != -1) {
            suffixname = filename.substring(filename.indexOf("."));
            if (suffixname.equals(".ppt")||suffixname.equals(".pptx")) {
                isppt = true;
            }
            return isppt;
        } else {
            return isppt;
        }
    }

    /**
     * ppt07转html
     * filename:要读取的文件所在文件夹
     * filepath:文件名
     * htmlname:生成html名称
     * path:html存放路径
     * imgpath:图片存放路径
     * */
    public static void pptTohtml07() throws IOException, TransformerException{
        String htmlname="exportPpt.html";
        // 读入PPT文件
        File file = new File("D:\\docx\\测试.pptx");
        boolean isppt = checkFile(file);
        if (!isppt) {
            System.out.println("The image you specify don't exit!");
        }
        FileInputStream is = new FileInputStream(file);
        XMLSlideShow ppt = new XMLSlideShow(is);
        is.close();

        Dimension pgsize = ppt.getPageSize();
        System.out.println(pgsize.width+"--"+pgsize.height);
        XSLFSlide[] pptPageXSLFSLiseList=ppt.getSlides();
        FileOutputStream out=null;
        String imghtml="";
        for (int i = 0; i < pptPageXSLFSLiseList.length; i++) {
            try {
                for(XSLFShape shape : pptPageXSLFSLiseList[i].getShapes()){
                    if(shape instanceof XSLFTextShape) {
                        XSLFTextShape tsh = (XSLFTextShape)shape;
                        for(XSLFTextParagraph p : tsh){
                            for(XSLFTextRun r : p){
                                r.setFontFamily("宋体");
                            }
                        }
                    }
                }
                BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
                Graphics2D graphics = img.createGraphics();
                // clear the drawing area
                graphics.setPaint(Color.white);
                graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

                // render
                pptPageXSLFSLiseList[i].draw(graphics);

                //
                String Imgname = "D:\\docx\\images\\"+ (i+1) + ".jpg";
                out = new FileOutputStream(Imgname);
                javax.imageio.ImageIO.write(img, "jpg", out);
                //图片在html加载路径
                String imgs="images/"+(i + 1) + ".jpg";
                imghtml+="<img src=\'"+imgs+"\' style=\'width:1200px;height:830px;vertical-align:text-bottom;\'><br><br><br><br>";
            } catch (Exception e) {
                System.out.println(e);
                System.out.println("第"+i+"张ppt转换出错");
            }
        }

        System.out.println("7success");
        DOMSource domSource = new DOMSource();
        StreamResult streamResult = new StreamResult(out);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer serializer = tf.newTransformer();
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.METHOD, "html");
        serializer.transform(domSource, streamResult);
        out.close();
        String ppthtml="<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"+imghtml+"</body></html>";
        FileUtils.writeStringToFile(new File("D:\\docx\\", htmlname), ppthtml, "utf-8");
    }

    public static void main(String[] args) throws Exception{
        pptTohtml07();
    }
}
